layui.use(['form','layer'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    //添加验证规则
    form.verify({
        sfz : function(value, item){
            var data;
            var id=$("#user_id").val();
            $.ajax({
                async: false,
                type: "POST",
                url: "/checkSfzUnique",
                data: "sfz="+value+"&id="+id,
                success: function(msg){
                    data=msg;
                }
            });
            if (data == "true") {
                return "身份证已经存在！";
            }
        }
    });
    form.on("submit(addUser)",function(data){
        var arr = new Array();
        $("input:checkbox[name='role']:checked").each(function(i){
            arr[i] = $(this).val();
        });
        data.field.sex=$("input:radio[name='sex']:checked").val();
        if (data.field.sex == undefined || data.field.sex==null) {
            top.layer.msg("请先选择性别！");
            return false;
        }
        if(arr.length<=0){
            top.layer.msg("请先选择角色！");
            return false;
        }
        data.field.role = arr.join(",");
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        // 实际使用时的提交信息
        $.post("/userAdd", {
            id: $("#user_id").val(),//id
            name: $(".name").val(),  //姓名
            email: $(".email").val(),  //邮箱
            sex: data.field.sex,  //性别
            status: data.field.status,    //用户状态
            mobile: $(".mobile").val(),  //手机
            role: data.field.role, //角色
            sfz:$('.identity').val()//身份证
        }, function (data) {
            if (data =="success") {
                top.layer.close(index);
                top.layer.msg("操作成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }else if (data == "error") {
                top.layer.msg("操作失败！");
            }else if (data == "logout") {
                top.layer.close(index);
                layer.closeAll("iframe");
                var curWwwPath = window.document.location.href;
                var pathName = window.document.location.pathname;
                var pos = curWwwPath.indexOf(pathName);
                var localhostPaht = curWwwPath.substring(0, pos);
                parent.parent.location.href = localhostPaht+"/logout";
            }
        });
/*        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("用户添加成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);*/
        return false;
    });
    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})
